<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>APP后台管理系统</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <i th:replace="~{common :: baseUrl}"></i>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    <form class="layui-form" action="">
        <input hidden name="id" th:value="${appInfo.id}">
    <!--        第一行-->
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">软件名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="softwareName" autocomplete="off" class="layui-input" th:value="${appInfo.softwareName}">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">apk名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="apkName" autocomplete="off" class="layui-input" th:value="${appInfo.apkName}">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">ORM版本</label>
                <div class="layui-input-inline">
                    <input type="text" name="supportRom" autocomplete="off" class="layui-input" th:value="${appInfo.supportRom}">
                </div>
            </div>

        </div>
        <!--        第二行 -->
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">界面语言</label>
                <div class="layui-input-inline">
                    <input type="text" name="interfaceLanguage" autocomplete="off" class="layui-input" th:value="${appInfo.interfaceLanguage}">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">软件大小</label>
                <div class="layui-input-inline">
                    <input type="text" name="softwareSize" autocomplete="off" class="layui-input" th:value="${appInfo.softwareSize}">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">所属平台</label>
                <div class="layui-input-inline">
                    <select name="flatformId" lay-verify="required" lay-search="">
                        <option value="">--请选择--</option>
                        <option th:each="f : ${flatformList}"  th:value="${f.valueId}" th:text="${f.valueName}"  th:selected="${appInfo.flatformId == f.valueId}"></option>
                    </select>
                </div>
            </div>

        </div>
        <!--        第三行 三级分类-->

        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">一级分类</label>
                <div class="layui-input-inline">
                    <select name="categoryLevel1" lay-filter="levelOne" id="levelOne" lay-verify="required" >
                        <option value="">--请选择--</option>
                        <option th:each="l : ${levelOneList}"  th:value="${l.id}" th:text="${l.categoryName}" th:selected="${appInfo.categoryLevel1 == l.id}"></option>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">二级分类</label>
                <div class="layui-input-inline">
                    <select name="categoryLevel2" lay-filter="levelTwo" id="levelTwo" lay-verify="required" >
                        <option value="">--请选择--</option>
                        <option th:each="l : ${levelTwoList}"  th:value="${l.id}" th:text="${l.categoryName}"  th:selected="${appInfo.categoryLevel2 == l.id}">
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">三级分类</label>
                <div class="layui-input-inline">
                    <select  name="categoryLevel3" lay-filter="levelThree" id="levelThree" lay-verify="required">
                        <option value="">--请选择--</option>
                        <option th:each="l : ${levelThreeList}"  th:value="${l.id}" th:text="${l.categoryName}" th:selected="${appInfo.categoryLevel3 == l.id}"></option>
                    </select>
                </div>
            </div>

        </div>

        <!--        第四行 文件上传-->
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">上传图片</label>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="uploadPic">上传图片</button>
                        <div class="layui-upload-list">
                            <input hidden name="picPath" id="picPath" th:value="${appInfo.picPath}">
                            <img width="100px" height="100px" class="layui-upload-img" id="showPic" th:src="${appInfo.picPath}">
                            <p id="uploadText"></p>
                        </div>
                    </div>
                </div>
            </div>

        </div>

        <!--        第五行 富文本编辑器-->
        <div class="layui-form-item">
            <label class="layui-form-label">APP简介</label>
            <div class="layui-input-block" style="width: 845px">
                <textarea lay-verify="appInfo" name="appInfo" id="appInfo" style="display: none;" th:text="${appInfo.appInfo}"></textarea>
            </div>
        </div>

        <!--        第六行  添加按钮-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit=""  lay-filter="go">添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>

</div>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/layui/echarts.js}"></script>
<script th:src="@{/layui/app.js}"></script>
<script>



    //JavaScript代码区域
    layui.use(['element','form','layedit','upload'], function(){
        var element = layui.element;
        var form = layui.form;
        var layedit = layui.layedit;
        var upload = layui.upload;

        var $ = layui.$;

        //监听一级分类下拉列表的改变
        form.on('select(levelOne)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            findLevelTwo(data,form,$);

        });

        //监听二级分类下拉列表的改变
        form.on('select(levelTwo)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            findLevelThree(data,form,$);

        });


        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadPic'
            ,url: baseUrl+'upload/pic'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#showPic').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 0){
                    return layer.msg('上传失败');
                }else {
                    $("#picPath").val(res.data.src)
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#uploadText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        //建立富文本编辑器   返回索引为核心对象  其他方法都通过该索引获取
        var appInfo = layedit.build('appInfo', {
            height: 133 //设置编辑器高度
        });
        //手动校验指定lay-verify="appInfo"的参数    即富文本编辑器的参数
        form.verify({
            appInfo: function (value, item){ //value：表单的值、item：表单的DOM对象
                // 将富文本编辑器的内容同步到textarea.   在提交表单时能拿到改该数据
                layedit.sync(appInfo);
                var content = layedit.getContent(appInfo);
                if (content==null || content == ''){
                    return "APP简介不能为空！！";
                }
            }
        });


        // 监听submit的单击事件
        form.on('submit(go)', function(data){
            // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            // console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            // console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var app = data.field;

            //发送ajax请求  编辑APP信息
            $.ajax({
                url:baseUrl+"dev/app/editAppInfo",
                data:app,
                type:"post",
                dataType:"json",
                success:function (result) {
                    if (result.code==0){
                        layer.open({
                            title: '编辑APP信息'
                            ,content: '恭喜你! 编辑APP信息！'
                            ,end:function () {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                        });
                    }else {
                        layer.open({
                            title: '编辑APP信息'
                            ,content: result.msg
                        });
                    }
                },
                error:function () {
                    layer.msg("服务器原地爆炸,请联系管理员!!!");
                }
            })





            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });







    });











</script>
</body>
</html>